<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\model\Admin as model;
use app\admin\validate\Admin as validate;
use Rych\Random\Random;
use think\App;
use think\facade\Hook;

class Admin extends Base{
    public function __construct(App $app = null){
        parent::__construct($app);
        $this->validate = new validate;
        $this->model = new model;
    }

    /**
     * 获取列表数据
     * @param array $where
     * @param string $sort
     * @param int $limit
     * @return \think\Response
     * @throws \think\exception\DbException
     */
    public function index($where=[],$sort='',$limit=8){
        $where = $this->_where($where);
        $order = $this->_order($sort);
        $list = $this->model->getList($where,$order,$limit,['guide','password','hash'],true);
        return $this->__s('',$list);
    }

    /**
     * 保存数据
     * @param int $id
     * @return \think\Response
     */
    public function save($id=0){
        $data = $this->request->post();
        if(!$this->validate->check($data)){
            return $this->__e($this->validate->getError());
        }
        if($id){
            $info = $this->model->get($id);
            if(!$info){
                return $this->__e('账号不存在');
            }
            if(isset($data['password']) && empty($data['password'])){
                $data['password'] = $this->_password($data['password'],$info['hash']);
            }
            if(!$info->save($data)){
                return $this->__e('操作失败');
            }
            $sql = $this->model->getLastSql();
            $params = [
                'aid'=>self::$uid,
                'action'=>"更新{$this->model->getTableComment()}，编号：{$id}",
                'sql'=>$sql
            ];
            Hook::listen('admin_log',$params);
            return $this->__s('操作成功');
        }else{
            $count = $this->model->where('username','eq',$data['username'])->count();
            if($count){
                return $this->__e('管理员已存在，请确认后再添加');
            }
            $hash = (new Random())->getRandomString('4');
            $data['password'] = $this->_password($data['password'],$hash);
            $data['hash'] = $hash;
            unset($data['id']);
            $id = $this->model->insertGetId($data);
            if(!$id){
                return $this->__e('操作失败');
            }
            $sql = $this->model->getLastSql();
            $params = [
                'aid'=>self::$uid,
                'action'=>'新增'."{$this->model->getTableComment()}，编号：{$id}",
                'sql'=>$sql
            ];
            Hook::listen('admin_log',$params);
            return $this->__s('操作成功');
        }
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function delete($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        if(!$info->delete()){
            return $this->__e('删除失败');
        }
        $sql = $info->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>'删除'."{$this->model->getTableComment()}，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('删除成功');
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function read($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    public function edit($id){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function update($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        $data = $this->request->put();
        if(!$info->save($data)){
            return $this->__e('操作失败');
        }
        $sql = $info->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>'删除'."{$this->model->getTableComment()}，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('操作成功');
    }
}